package member

import (
	"fmt"
	"woo-server/coms/structs"
	"woo-server/coms/utils"
	"woo-server/models/db"
	"woo-server/models/db/content"
)

// 权限表
type GRADE struct {
	*structs.Common `gorm:"-" json:"-"`
	GradeId         uint64               `gorm:"primary_key;AUTO_INCREMENT;column:grade_id;"` // 自增主键
	GradeName       string               `gorm:"column:grade_name;size:50;"`                  // 等级名称
	FileId          uint64               `gorm:"column:image_id;size:50;"`                    // 图片id
	Weight          uint                 `gorm:"column:weight;size:150;"`                     // 等级权重(1-99)
	Describe        string               `gorm:"column:describe;size:150;"`                   // 条件描述
	Upgrade         string               `gorm:"column:upgrade;size:150;"`                    // 升级条件
	Discount        float64              `gorm:"column:discount;size:150;"`                   // 等级权益(折扣率0-100)
	GradeStatus     uint                 `gorm:"column:grade_status;size:150;"`               // 状态(1启用 0禁用)
	IsDelete        int                  `gorm:"column:describe;size:150;"`                   // 是否删除
	Image           []content.UploadFile `gorm:"foreignKey:FileId;references:FileId"`         // 图片模型数组返回
	db.Basics
}

// 多选框结构体
type OPTIONS struct {
	//Select        []Options
	SelectOptions interface{}
}

func (r *GRADE) Column() []structs.ColumnCommon {

	result := make([]structs.ColumnCommon, 0)
	result = append(result, structs.ColumnCommon{Title: "ID", DataIndex: "GradeId"})
	result = append(result, structs.ColumnCommon{Title: "封面", DataIndex: "Image"})
	result = append(result, structs.ColumnCommon{Title: "等级名称", DataIndex: "GradeName"})
	result = append(result, structs.ColumnCommon{Title: "等级权重(1-99)", DataIndex: "Weight"})
	result = append(result, structs.ColumnCommon{Title: "描述", DataIndex: "Describe"})
	result = append(result, structs.ColumnCommon{Title: "升级条件", DataIndex: "Upgrade"})
	result = append(result, structs.ColumnCommon{Title: "等级权益(折扣率0-100)", DataIndex: "Discount"})
	result = append(result, structs.ColumnCommon{Title: "操作", DataIndex: "Operation"})
	return result
}

func (r *GRADE) Forms() []structs.FormsCommon {
	result := make([]structs.FormsCommon, 0)
	//rModel := NewROLE()
	//Model := NewMenus(r.Common)
	//_, list := Model.GetAllMenu(true)
	////var options OPTIONS
	result = append(result, structs.FormsCommon{Key: "GradeId", Label: "主键ID", Type: "hidden", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "GradeName", Label: "等级名称", Type: "input", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "FileId", Label: "封面", Type: "onlyUploadImage", Options: "Image"})
	result = append(result, structs.FormsCommon{Key: "Weight", Label: "等级权重(1-99)", Type: "number", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "Describe", Label: "描述", Type: "textarea", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "Upgrade", Label: "升级条件", Type: "textarea", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "Discount", Label: "等级权益(折扣率0-100)", Type: "number", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "GradeStatus", Label: "状态", Type: "switch", Options: []string{}})
	return result
}

// 表名
func (r *GRADE) TableName() string {
	return "woo_member_grade"
}

// 初始化
func NewGrade(com *structs.Common) *GRADE {
	d := &GRADE{}
	d.Common = com
	if com.Claim != nil {
		//d.CreateUser = com.Claim.Id
		//d.UpdateUser = com.Claim.Id
	}
	return d
}
func (r *GRADE) GetConfig() structs.CFConfig {
	list := structs.CFConfig{}
	list.Column = r.Column()
	list.Forms = r.Forms()
	return list
}

// 创建
func (r *GRADE) Create() error {
	return utils.GORM.Debug().Table(r.TableName()).Create(r).Error
}

// 批量创建
func (r *GRADE) BatchCreate(data []*GRADE) error {
	return utils.GORM.Debug().Table(r.TableName()).Create(data).Error
}

// 获取分页列表及总数
func (r *GRADE) GetListAndTotal(search string, page int, pageSize int) ([]*GRADE, int64) {
	list := make([]*GRADE, 0)
	var total int64 = 0

	// 查询
	query := utils.GORM.Debug().Table(r.TableName())

	if len(search) > 0 {
		search = fmt.Sprintf("%%%s%%", search)
		query = query.Where("name like ?", search)
	}
	// 不查询软删除数据
	query = query.Where("is_delete = 0")
	// 总数
	query.Count(&total)

	// 列表
	offset := (page - 1) * pageSize
	query.Preload("Image").Limit(pageSize).Offset(offset).Find(&list)

	return list, total
}

// 获取单挑数据
func (r *GRADE) GetFirst(where interface{}, field ...interface{}) *GRADE {
	//info := make([]*GRADE, 0)
	// 查询
	query := utils.GORM.Debug().Table(r.TableName())

	query = query.Where(where, field)

	// 总数
	//query.Count(&total)

	// 列表
	//offset := (page - 1) * pageSize
	query.First(&r)
	return r
}

//func (r *GRADE) SetOption(List []*GRADE) []Options {
//	var AdminGRADEMenus []Options
//	for _, v := range List {
//		var option Options
//		option.Value = v.AdminGRADEId
//		option.Label = v.Name
//		AdminGRADEMenus = append(AdminGRADEMenus, option)
//	}
//	return AdminGRADEMenus
//}

// 更新文章
func (r *GRADE) Save() error {
	// 更新
	return utils.GORM.Debug().Table(r.TableName()).Save(r).Error
}

// 删除文章
func (r *GRADE) Delete() error {
	return utils.GORM.Debug().Table(r.TableName()).Delete(r).Error
}

// 获取单条数据
func (r *GRADE) First() error {
	return utils.GORM.Debug().Table(r.TableName()).First(r).Error
}

func (r *GRADE) SetOption(List []*GRADE) []structs.Options {
	var AdminRoleMenus []structs.Options
	for _, v := range List {
		var option structs.Options
		option.Value = v.GradeId
		option.Label = v.GradeName
		AdminRoleMenus = append(AdminRoleMenus, option)
	}
	return AdminRoleMenus
}
